//
//  SlashEMAppDelegate.m
//  SlashEM
//
//  Created by dirk on 12/30/09.
//  Copyright Dirk Zimmermann 2009. All rights reserved.
//

//  This file is part of Slash'EM.
//
//  Slash'EM is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, version 2 of the License only.
//
//  Slash'EM is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Slash'EM.  If not, see <http://www.gnu.org/licenses/>.

#import "SlashEMAppDelegate.h"
#import "MainViewController.h"
#import "winiphone.h"

#include <sys/stat.h>

extern int unixmain(int argc, char **argv);

@implementation SlashEMAppDelegate

@synthesize window;
@synthesize mainViewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
	[application setStatusBarHidden:YES animated:YES];

	mainViewController.view.frame = [[UIScreen mainScreen] bounds];
	[window addSubview:mainViewController.view];
    [window makeKeyAndVisible];
	
	slashemThread = [[NSThread alloc] initWithTarget:self selector:@selector(slashemMainLoop:) object:nil];
	[slashemThread start];
}


- (void) slashemMainLoop:(id)arg {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	char *argv[] = {
		"Slash'EM"	
	};
	
	// create necessary directories
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *baseDirectory = [paths objectAtIndex:0];
	NSLog(@"baseDir %@", baseDirectory);
	setenv("NETHACKDIR", [baseDirectory cStringUsingEncoding:NSASCIIStringEncoding], 1);
	NSString *saveDirectory = [baseDirectory stringByAppendingPathComponent:@"save"];
	mkdir([saveDirectory cStringUsingEncoding:NSASCIIStringEncoding], 0777);
	
	// show directory (for debugging)
	for (NSString *filename in [[NSFileManager defaultManager] enumeratorAtPath:baseDirectory]) {
		NSLog(@"%@", filename);
	}
	
	// set plname (very important for save files and getlock)
	[[NSUserName() capitalizedString] getCString:plname maxLength:PL_NSIZ encoding:NSASCIIStringEncoding];
	
	// call Slash'EM
	unixmain(1, argv);
	
	// clean up thread pool
	[pool release];
}

- (void)applicationWillTerminate:(UIApplication *)application {
	if (!program_state.gameover && program_state.something_worth_saving) {
		dosave0();
	}
}

- (void)dealloc {
    [window release];
	[slashemThread release];
    [super dealloc];
}

@end
